# 计算类
import pandas as pd

from dao.writeToFile import writeToFile
from pyeCharts.pyeCharts import pyeCharts


class calculate:
    def __init__(self):
        pass

    # 对各地区历年的数据求加权平均和
    @staticmethod
    def calculateDataForCommon(year_data, filename):
        # 读取综合数据
        columns = list(year_data.columns)
        index = list(year_data.index)
        # 权重
        weights = [
            0.31,
            0.19,
            0.15,
            0.1,
            0.08,
            0.06,
            0.08,
            0.02,
            0.01,
            0.00  # 总人口
        ]
        weights_sum = []  # 权重和
        year_data['GDP'] = year_data['GDP'] / year_data['总人口']  # GDP 对总人口做下除法
        year_data.to_excel('Excel\\地区GDP对总人口做除法后.xlsx')
        for index, row in year_data.iterrows():
            weight_sum = row.mul(weights).sum()
            weights_sum.append(weight_sum)
        avg_data = {'加权平均': weights_sum}
        year_data['加权平均'] = weights_sum
        writeToFile.writeDataForCommonToExcel(year_data, filename)

    # 统计各地区近 10 年的发展指数
    @staticmethod
    def calculateDataForDevelopment():
        year_list = ['2014', '2015', '2016', '2017', '2018', '2019', '2020', '2021', '2022']
        # 获取 index
        index = list(pd.read_excel('Excel\\各地区2014年综合数据统计.xlsx', index_col=0).index)
        year_data = pd.DataFrame(index=index, columns=year_list)
        # 遍历每一年
        for year in year_list:
            filename = f'Excel\\各地区{year}年综合数据统计.xlsx'
            data = pd.read_excel(filename, index_col=0)['加权平均']  # 获取存放加权平均数的那一列
            year_data[year] = data

        filename = '各地区近10年发展指数'
        # 持久化到本地
        writeToFile.writeDataForCommonToExcel(year_data, filename)
        # 生成折线图
        pyeCharts.drawLineForCommon(year_data, filename)
        # 生成地图
        timeline = pyeCharts.drawMapForDevelopment(year_data, filename)
        # 生成发展指数条形图
        c = pyeCharts.drawBarForDevelopment(year_data, filename)
        return c
